package com.example.cxx.controller;

import com.example.cxx.Utils.CookieUtil;
import com.example.cxx.mapper.ScoreSubMapper;
import com.example.cxx.pojo.Result;
import com.example.cxx.pojo.Score;
import com.example.cxx.pojo.ScoreList;
import com.example.cxx.pojo.YuMing;
import org.apache.ibatis.annotations.Param;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("course/api/*")
public class TestController {
    public Integer scoreNum=0;

    @GetMapping("/test")
    public Result testCookie(String cookie) {
        if("".equals(cookie)){
            return Result.error("LOGIN-FAIL");
        }

        try {
            Document index= Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Module/NetEa/Score/Show/Default.aspx").cookies(CookieUtil.convertCookie(cookie)).get();
            if(index.getElementById("Account")!=null&&"".equals(index.getElementById("Account").val())){
                return Result.error("LOGIN-FAIL");
            }
            //学分统计/必修课/专业选修课/文化素质课/公共选修课/平均学分绩点
            Score score=new Score();
            List<ScoreList> scoreLists=new ArrayList<>();
            Element element=index.getElementById("tabCredit").child(0);
            Elements scoreTerms=index.getElementById("gvHeight").getElementsByClass("row");
            Elements scoreTerms2=index.getElementById("gvHeight").getElementsByClass("row1");
            Elements row1=element.getElementsByClass("row1");
            Elements row=element.getElementsByClass("row");
            for (Element element1 : row1) {
                score.setPlanScoreCount(element1.child(0).text());
                score.setPlanCompulsory(element1.child(1).text());
                score.setPlanMajorElectives(element1.child(2).text());
                score.setPlanCulturalLiteracyLessons(element1.child(3).text());
                score.setPlanPublicElectives(element1.child(4).text());
                score.setGradePointAverage(element1.child(5).text());
            }
            for (Element element1 : row) {
                score.setScoreCount(element1.child(0).text());
                score.setCompulsory(element1.child(1).text());
                score.setMajorElectives(element1.child(2).text());
                score.setCulturalLiteracyLessons(element1.child(3).text());
                score.setPublicElectives(element1.child(4).text());

            }
                addList(scoreLists, scoreTerms);
                addList(scoreLists, scoreTerms2);
//                try{
//                    if(scoreSubMapper.selectScoreUpdateByUserAccount(Integer.valueOf(userAccount))){
//                        scoreSubMapper.updateScoreUpdate(Integer.valueOf(userAccount),scoreNum);
//                    }
//                    else {
//                        scoreSubMapper.insertScoreUpdate(Integer.valueOf(userAccount),scoreNum);
//                    }
//                }
//                finally {
                    Collections.sort(scoreLists);
                    score.setScoreLists(scoreLists);
                    return Result.success(score);
              //  }


        }
        catch (Exception e){
            return Result.error("LOGIN-FAIL");
        }

    }
    public Result testCookie2(String cookie) throws IOException {
        if("".equals(cookie)){
            return Result.error("LOGIN-FAIL");
        }

        try {
            Document index= Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Module/NetEa/Score/Show/Default.aspx").cookies(CookieUtil.convertCookie2(cookie)).get();
            if(index.getElementById("Account")!=null&&"".equals(index.getElementById("Account").val())){
                return Result.error("LOGIN-FAIL");
            }
            //学分统计/必修课/专业选修课/文化素质课/公共选修课/平均学分绩点
            Score score=new Score();
            List<ScoreList> scoreLists=new ArrayList<>();
            Element element=index.getElementById("tabCredit").child(0);
            Elements scoreTerms=index.getElementById("gvHeight").getElementsByClass("row");
            Elements scoreTerms2=index.getElementById("gvHeight").getElementsByClass("row1");
            Elements row1=element.getElementsByClass("row1");
            Elements row=element.getElementsByClass("row");
            for (Element element1 : row1) {
                score.setPlanScoreCount(element1.child(0).text());
                score.setPlanCompulsory(element1.child(1).text());
                score.setPlanMajorElectives(element1.child(2).text());
                score.setPlanCulturalLiteracyLessons(element1.child(3).text());
                score.setPlanPublicElectives(element1.child(4).text());
                score.setGradePointAverage(element1.child(5).text());
            }
            for (Element element1 : row) {
                score.setScoreCount(element1.child(0).text());
                score.setCompulsory(element1.child(1).text());
                score.setMajorElectives(element1.child(2).text());
                score.setCulturalLiteracyLessons(element1.child(3).text());
                score.setPublicElectives(element1.child(4).text());

            }
            addList(scoreLists, scoreTerms);
            addList(scoreLists, scoreTerms2);
            Collections.sort(scoreLists);
            score.setScoreLists(scoreLists);
            return Result.success(score);
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.error("LOGIN-FAIL");
        }

    }
    public void addList(List<ScoreList> scoreLists, Elements scoreTerms) {
        for (Element scoreTerm : scoreTerms) {
            ScoreList scoreList=new ScoreList();
            scoreList.setYear(scoreTerm.child(1).text());
            scoreList.setTerm(scoreTerm.child(2).text());
            scoreList.setSubjectName(scoreTerm.child(4).text());
            scoreList.setExamType(scoreTerm.child(6).text());
            scoreList.setExamTime(scoreTerm.child(7).text());
            scoreList.setScorePoint(scoreTerm.child(9).text());
            scoreList.setSubjectType(scoreTerm.child(11).text());
            scoreList.setExamScore(scoreTerm.child(13).text());
            scoreList.setExamPoint(scoreTerm.child(14).text());
            scoreLists.add(scoreList);
            scoreNum++;
        }

    }
}
